<template>
  <div class="head-box" style="">
    <!--<div class="head-org">-->
    <!--  <Icon icon="eos-icons:organization" />-->
    <!--  全部组织-->
    <!--</div>-->
    <div class="head-status">
      <div>显示停用</div>
      <el-switch v-model="showStatus" @change="switchChange"/>
    </div>
  </div>
  <div class="head-container">
    <el-input v-model="deptName" class="mb-20px" clearable placeholder="请输入部门名称">
      <template #prefix>
        <Icon icon="ep:search" />
      </template>
    </el-input>
  </div>
  <div class="head-container">
    <el-tree
      ref="treeRef"
      :data="deptList"
      :expand-on-click-node="false"
      :filter-node-method="filterNode"
      :props="defaultProps"
      :default-expand-all="false"
      highlight-current
      node-key="id"
      @node-click="handleNodeClick"
    />
  </div>
</template>

<script lang="ts" setup>
import { ElTree } from 'element-plus'
import * as DeptApi from '@/api/system/dept'
import { defaultProps, handleTree } from '@/utils/tree'

defineOptions({ name: 'SystemUserDeptTree' })

const showStatus = ref(false)
const deptName = ref('')
const deptList = ref<Tree[]>([]) // 树形结构
const treeRef = ref<InstanceType<typeof ElTree>>()
const queryParams = reactive({
  "name": '',
  "status": 0
})

/** 获得部门树 */
const getTree = async () => {
  const res = await DeptApi.getDeptPage(queryParams)
  deptList.value = []
  deptList.value.push(...handleTree(res))
}

/** 基于名字过滤 */
const filterNode = (name: string, data: Tree) => {
  if (!name) return true
  return data.name.includes(name)
}

/** 机构状态切换 */
const switchChange = async (val: any) => {
  if (val) {
    queryParams.status = null;
  } else {
    queryParams.status = 0;
  }
  const res = await DeptApi.getDeptPage(queryParams)
  deptList.value = []
  deptList.value.push(...handleTree(res))
  // 调用父组件方法
  emits('switchStatue', val);
}

/** 处理部门被点击 */
const handleNodeClick = async (row: { [key: string]: any }) => {
  emits('node-click', row)
}
const emits = defineEmits(['node-click'])

/** 监听deptName */
watch(deptName, (val) => {
  treeRef.value!.filter(val)
})

/** 初始化 */
onMounted(async () => {
  await getTree()
})
</script>
